package com.fuang.controller;

import com.fuang.bo.UserBO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.Request;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Fuang on 2017-12-21.
 */
@RestController
public class HelloController {
    private static final Logger LOG = LoggerFactory.getLogger(HelloController.class);

    @Autowired
    private DiscoveryClient client;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello(){
        LOG.info(client.getLocalServiceInstance().getHost() + ":" + client.getLocalServiceInstance().getServiceId());

        return client.getLocalServiceInstance().getHost() + ":" + client.getLocalServiceInstance().getServiceId() + "Hello!";
    }

    @RequestMapping(value = "/hello1", method = RequestMethod.GET)
    public String hello1(@RequestParam("name") String name){
        return "Hello " + name;
    }

    @RequestMapping(value = "/hello2", method = RequestMethod.GET)
    public UserBO hello2(@RequestHeader("name") String name, @RequestHeader("age") Integer age){
        return new UserBO(name, age);
    }

    @RequestMapping(value = "/hello3", method = RequestMethod.POST)
    public String hello3(@RequestBody UserBO userBO){
        return "Hello " + userBO.getName() + "," + "age=" + userBO.getAge();
    }

    @RequestMapping(value = "/test")
    public String test(HttpServletRequest request) {
        LOG.info("requestUrl=" + request.getRequestURL() + ", RemoteHost" + request.getRemoteHost());
        return "requestUrl=" + request.getRequestURL() + ", RemoteHost" + request.getRemoteHost();
    }
}
